package com.leetcode.slideWindow;

public class MergeArray {
    public static void main(String[] args) {

    }

    public void merge2(int[] nums1, int m, int[] nums2, int n) {
        int[] sorted = new int[m+n];
        int ptr1 = 0;
        int ptr2 = 0;
        int cur = 0;
        while (ptr1 < m || ptr2 < n) {
            if (ptr1 == m) {
                cur = nums2[ptr2++];
            } else if(ptr2 == n) {
                cur = nums1[ptr1++];
            } else if (nums1[ptr1] <= nums2[ptr2]) {
                cur = nums1[ptr1++];
            } else {
                cur = nums2[ptr2++];
            }
            sorted[ptr1 + ptr2 - 1] = cur;
        }
        System.arraycopy(sorted, 0, nums1, 0, sorted.length);
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int ptr1 = m - 1;
        int ptr2 = n - 1;
        int cur = 0;
        while (ptr1 >= 0 || ptr2 >= 0) {
            if (ptr1 < 0) {
                cur = nums2[ptr2--];
            } else if(ptr2 < 0) {
                cur = nums1[ptr1--];
            } else if(nums1[ptr1] >= nums2[ptr2]) {
                cur = nums1[ptr1--];
            } else {
                cur = nums2[ptr2--];
            }
            nums1[ptr1 + ptr2 + 2] = cur;
        }
    }
}
